// pages/ucenter/code/index.js
const app = getApp();
var md5 = require("../../../utils/md5.js");
import QR from '/../../../utils/wxqrcode.js';



Page({

  /**
   * 页面的初始数据
   */
  data: {
    qrcode: '',
    shop_name: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var size = this.getQRCodeSize();
    // var shop_id = app.globalData.shopId;
    // //var text = 'http://wx.zonuo-china.com/?shop_id=' + shop_id;
    // var text = 'http://wx.zonuo-china.com/?shopid=123';
    // console.log(text);
    //this.createQRCode(text, size);
    this.getQR();
    this.getShopInfo();

  },

  //获取小程序二维码
  getQR: function(){

    var service = 'User.WxShopShareQR';
    var id_name = 'shop_id';
    var id_value = app.globalData.shopId;
    var sign = md5.md5(id_name + id_value + service);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        id_name: id_name,
        id_value: id_value,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          this.setData({
            qrcode: result.info
          })
        } else {
          wx.showToast({
            title: '获取失败',
            icon: 'success',
            duration: 2000
          })
        }
      },
    })

  },

  //获取店铺信息
  getShopInfo:function(){

    var service = 'Shop.GetShopInfo';
    var openid = app.globalData.openId;
    var user_id = app.globalData.userId;
    var shop_id = app.globalData.shopId;
    var sign = md5.md5(openid + service + shop_id + user_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        openid: openid,
        shop_id: shop_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          this.setData({
            shop_name: result.info.shop_name,
          })
        } else {
          wx.showToast({
            title: '获取失败',
            icon: 'success',
            duration: 2000
          })
        }
      },
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

// getQRCodeSize: function () {
//   var size = 0; try {
//   var res = wx.getSystemInfoSync();
//   var scale = 750 / 278;
//   var width = res.windowWidth / scale;
//     size = width;
//   } catch (e) {
//   }
//   return size;
// },

// createQRCode: function (text, size) {
//   let that = this
//   let _img = QR.createQrCodeImg(text, {
//     size: parseInt(size)
//   })
//   that.setData({
//     'qrcode': _img
//   })
// },
  
downloadFile:function(){
  var url = this.data.qrcode;
  // 下载文件  
  wx.downloadFile({
    url: url,
    success: function (res) {
      console.log("下载文件：success");
      console.log(res);
      // 保存图片到系统相册  
      wx.saveImageToPhotosAlbum({
        filePath: res.tempFilePath,
        success(res) {
          console.log("保存图片：success");
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000
          })
        },
        fail(res) {
          console.log("保存图片：fail");
          console.log(res);
        }
      })
    },
    fail: function (res) {
      console.log("下载文件：fail");
      console.log(res);
    }
  })  
}

})